package top.codestudy.storageandroid10;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SharedPreferencesActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "SharedPreferences";
    private EditText mEtSaveName;
    private Button mBtnSaveName;
    private Button mBtnGetNameValue;
    private Button mBtnDelNameValue;
    private TextView mTvShowLog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
        initView();
        initSharedPreferences();
    }
    private SharedPreferences chencheng;
    private SharedPreferences preferences;
    private SharedPreferences defaultSharedPreferences;
    private void initSharedPreferences() {
        //获取SharedPreferences的三种方式,
        //1.Context.getSharedPrefences(String key,int mode)
        Context context = this;
        chencheng = context.getSharedPreferences("chencheng", Context.MODE_PRIVATE);
        //2.getPreferences(int mode)
        preferences = getPreferences(Context.MODE_PRIVATE);
        //3.PreferenceManager.getDefaultSharedPreferences()
        defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        /**
         * 三者之间的区别：
         * 第一种方式可以自定义文件名，属于整个应用程序，可以创建多个SharedPreferences，
         * 第二个方式创建的本Activity拥有的SharedPreferences，文件名为此Activity的名字
         * 第三种方式创建的是属于整个应用程序的SharedPreferences，文件名是这样的形式 =》 context.getPackageName() + "_preferences"
         */

    }

    private void initView() {
        mEtSaveName = findViewById(R.id.et_save_name);
        mBtnSaveName = findViewById(R.id.btn_save_name);
        mBtnGetNameValue = findViewById(R.id.btn_get_name_value);
        mBtnDelNameValue = findViewById(R.id.btn_del_name_value);
        mBtnSaveName.setOnClickListener(this);
        mBtnGetNameValue.setOnClickListener(this);
        mBtnDelNameValue.setOnClickListener(this);

        mTvShowLog = findViewById(R.id.tv_show_log);


    }

    public static void goSharedPreferencesActivity(Context activity){
        Intent intent = new Intent(activity,SharedPreferencesActivity.class);
        activity.startActivity(intent);
    }

    public String getEtSaveName(){
        return mEtSaveName.getText().toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_save_name:
                //保存键为name 的值，如果name键已经存在，则修改name的值
                createKey();
                break;
            case R.id.btn_get_name_value:
                //获取键name的值
                getKeyValue();
                break;
            case R.id.btn_del_name_value:
                //删除键为name的值
                delKey();
                break;
        }
    }

    private void delKey() {
        boolean name = chencheng.contains("name");
        Log.i(TAG, "getKeyValue: name 存在 ？" + name);
        if (name){
            SharedPreferences.Editor edit = chencheng.edit();
            edit.remove("name");
            edit.commit();
            Log.i(TAG, "delKey: ok!");
        }else {
            showShort("name 不存在！！");
            Log.i(TAG, "delKey:name 不存在！！ ");
        }
    }

    private void getKeyValue() {
        boolean name = chencheng.contains("name");
        Log.i(TAG, "getKeyValue: name 存在 ？" + name);
        if (name){
            String string = chencheng.getString("name", "null");
            Log.i(TAG, "getKeyValue: name 的值为 ： " + string);
        }else {
            showShort("name 不存在！！");
            Log.i(TAG, "getKeyValue: name 不存在！！");
        }
    }
    //为了简化代码，我们默认键为 name
    private void createKey() {
        boolean name = chencheng.contains("name");
        Log.i(TAG, "createKey: name is contain ? " + name);
        if (!name){
            SharedPreferences.Editor edit = chencheng.edit();
            edit.putString("name",getEtSaveName());
            edit.commit();
            Log.i(TAG, "createKey: set key");
        }else {
            SharedPreferences.Editor edit = chencheng.edit();
            edit.putString("name",getEtSaveName());
            edit.commit();
            Log.i(TAG, "createKey: change key");
        }
    }

    public void showShort(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        Log.d(TAG, "showShort: " + msg);
    }

}